unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ToolWin, ComCtrls, ExtCtrls;
type
TForm1 = class(TForm)
RadioGroup1: TRadioGroup;
procedure FormCreate(Sender: TObject);
procedure FormPaint(Sender: TObject);
procedure RadioGroup1Click(Sender: TObject);
private
{ Private declarations }
procedure TransparentBmp(Dc: HDC; Bitmap: HBitmap; xStart: Integer; yStart: Integer; trColor: TColor);
public
{ Public declarations }
Bmp: TBitmap;
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
{$R bmpres.res}
procedure TForm1.FormCreate(Sender: TObject);
begin
Bmp := TBitmap.Create;
Bmp.LoadFromResourceName(HInstance,'MAP');
end;
procedure TForm1.FormPaint(Sender: TObject);
begin
case Radiogroup1.ItemIndex of
0: Canvas.Draw(10,10,Bmp);
1: TransparentBmp(Canvas.Handle,Bmp.Handle,10,10,clWhite);
2: TransparentBlt(Canvas.Handle,10,10,100,100,Bmp.Canvas.Handle,0,0,bmp.Width,
bmp.Height,clWhite);
else Canvas.Draw(10,10,Bmp);
end;
end;
procedure TForm1.TransparentBmp(Dc: HDC; Bitmap: HBitmap; xStart: Integer;
yStart: Integer; trColor: TColor);
var bm: Windows.TBitmap;
hDcTemp: HDC;
ptSize: TPoint;
hDcBack, hDcObject, hDcMem, hDcSave: hDc;
bmAndBack, bmAndObject, bmAndMem, bmSave: HBitmap;
bmBackOld, bmObjectOld, bmMemOld, bmSaveOld: HBitmap;
sColor: TColorRef;
begin
hDcTemp := CreateCompatibleDc(Dc);
SelectObject(hDcTemp,Bitmap);
GetObject(Bitmap,Sizeof(bm),@bm);
ptSize.x := bm.bmWidth;
ptSize.y := bm.bmHeight;
//создает временные Dc
hDcBack := CreateCompatibleDc(Dc);
hDcObject := CreateCompatibleDc(Dc);
hDcMem := CreateCompatibleDc(Dc);
hDcSave := CreateCompatibleDc(Dc);
//создаем битмапы для каждого DC
bmAndBack := CreateBitmap(ptSize.x,ptSize.y,1,1,nil);
bmAndObject := CreateBitmap(ptSize.x,ptSize.y,1,1,nil);
bmAndMem := CreateCompatibleBitmap(Dc,ptSize.x,ptSize.y);
bmSave := CreateCompatibleBitmap(Dc,ptSize.x,ptSize.y);
//каждому Dc выбрать витмап для сохранения данных пикселей
bmBackOld := SelectObject(hDcBack,bmAndBack);
bmObjectOld := SelectObject(hDcObject,bmAndObject);
bmMemOld := SelectObject(hDcMem,bmAndMem);
bmSaveOld := SelectObject(hDcSave,bmSave);
//установка соответствующего mapping mode
SetMapMode(hDcTemp,GetMapMode(Dc));
//сохраняем витмап в темпе, потому что он будет переписан
BitBlt(HDcSave,0,0,ptSize.x,ptSize.y,HDcTemp,0,0,SRCCOPY);
//устанавливаем задний план
sColor := SetBkColor(hDcTemp,trColor);
//создаем маску
BitBlt(hDcObject,0,0,ptSize.x,ptSize.y,hDcTemp,0,0,SRCCOPY);
//устанавливаем задний план обратно в оригинал
SetBkColor(hDcTemp,sColor);
//готовим инверсную маску
BitBlt(hDcBack,0,0,ptSize.x,ptSize.y,hDcObject,0,0,NOTSRCCOPY);
//копируем задний план основного Dc
BitBlt(hDcMem,0,0,ptSize.x,ptSize.y,Dc,xStart,yStart,SRCCOPY);
//
BitBlt(hDcMem,0,0,ptSize.x,ptSize.y,hDcObject,0,0,SRCAND);
//
BitBlt(hDcTemp,0,0,ptSize.x,ptSize.y,hDcBack,0,0,SRCAND);
//
BitBlt(hDcMem,0,0,ptSize.x,ptSize.y,hDcTemp,0,0,SRCPAINT);
//
BitBlt(Dc,xStart,yStart,ptSize.x,ptSize.y,hDcMem,0,0,SRCCOPY);
//
BitBlt(hDcTemp,0,0,ptSize.x,ptSize.y,hDcSave,0,0,SRCCopy);
DeleteObject(SelectObject(hDcBack,bmBackOld));
DeleteObject(SelectObject(hDcObject,bmObjectOld));
DeleteObject(SelectObject(hDcMem,bmMemOld));
DeleteObject(SelectObject(hDcSave,bmSaveOld));
DeleteDc(hDcBack);
DeleteDc(hDcObject);
DeleteDc(hDcMem);
DeleteDc(hDcSave);
DeleteDc(hDcTemp);
end;
procedure TForm1.RadioGroup1Click(Sender: TObject);
begin
Invalidate;
end;
end.
|